﻿using AutoMapper;
using Modules;
using Modules.Commander;
using Modules.Constructor;
using PmSoft;
using PmSoft.ApplicationContext;

namespace ManagementApi.Models.Commander
{
    public class CommanderDto
    {
        /// <summary>
        /// 指挥部ID 
        ///</summary>
        public int CommanderId { get; set; }
        /// <summary>
        /// 指挥部名称 
        ///</summary>
        public string Name { get; set; }
        /// <summary>
        /// 建设单位名称
        /// </summary>
        public string ConstructorName { get; set; }
        /// <summary>
        /// 指挥者名称 
        ///</summary>
        public string Leader { get; set; }
        /// <summary>
        /// 指挥者手机号码 
        ///</summary>
        public string Mobile { get; set; }
        /// <summary>
        /// 指挥者身份证号码 
        ///</summary>
        public string Cardno { get; set; }
        /// <summary>
        /// 创建时间 
        /// 默认值: CURRENT_TIMESTAMP
        ///</summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 项目集合
        /// </summary>
        public IEnumerable<CommanderProjectEditForm> Projects { get; set; }
    }

    /// <summary>
    /// 扩展
    /// </summary>
    public static partial class CommanderExceptions
    {

        public static CommanderDto AsDto(this CommanderInfo entity,IApplicationContext applicationContext)
        {
            CommanderDto commanderDto = new CommanderDto();
            var mapper = ServiceAccessor.GetService<IMapper>();
            var commanderProjectService = ServiceAccessor.GetService<CommanderProjectService>();
            mapper.Map(entity, commanderDto);
            commanderDto.Projects = commanderProjectService.GetCommanderProjectsByCommanderId(entity.CommanderId).AsForm(applicationContext);
            var constructorService = applicationContext.GetService<ConstructorService>();
            var constructor = constructorService.GetConstructorInfo(entity.ConstructorId);
            commanderDto.ConstructorName = constructor?.Name ?? string.Empty;
            return commanderDto;
        }

        /// <summary>
        /// 下拉列表
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static LabelValue<int> AsPullDownDto(this CommanderInfo entity)
        {
            LabelValue<int> data = new LabelValue<int>();
            data.Value = entity.CommanderId;
            data.Label = entity.Name;
            return data;
        }
    }
}
